package com.shxc.smarttrade.login.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.shxc.smarttrade.R;
import com.shxc.smarttrade.base.NoActionBarBaseActivity;
import com.shxc.smarttrade.currency.model.CurrencyModel;
import com.shxc.smarttrade.info.Contents;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisModel;
import com.shxc.smarttrade.utils.analysis.JsonAnalysisUtils;
import com.shxc.smarttrade.utils.AppUtils;
import com.shxc.smarttrade.utils.MyOkHttpUtils;
import com.shxc.smarttrade.utils.ToastUtils;

import org.xchris.view.annotation.ContentView;
import org.xchris.view.annotation.Event;
import org.xchris.view.annotation.ViewInject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 重置密码
 */
@ContentView(R.layout.activity_login_forgetpass)
public class LoginForgetPassActivity extends NoActionBarBaseActivity {

    @ViewInject(R.id.email_warning)
    TextView email_warning;
    @ViewInject(R.id.email_edittext)
    EditText email_edittext;
    @ViewInject(R.id.email_line)
    View email_line;
    @ViewInject(R.id.input_view)
    View input_view;
    @ViewInject(R.id.notice1_view)
    View notice1_view;
    @ViewInject(R.id.notice2_view)
    View notice2_view;
    @ViewInject(R.id.email_textview)
    TextView email_textview;
    @ViewInject(R.id.success_view)
    View success_view;
    @ViewInject(R.id.find_textview)
    View find_textview;
    @ViewInject(R.id.progress_line)
    View progress_line;

    private int RED = Color.parseColor("#E24C4F");
    private int BLUE = Color.parseColor("#141F2B");
    private int GRAY = Color.parseColor("#B3B3B3");
    private int UNCLICK = Color.parseColor("#50141F2B");

    private InputMethodManager imm;
    private String email = "";

    @Event({R.id.close_imageview,R.id.find_textview,R.id.submit_textview})
    private void onClickk(View view) {
        switch (view.getId()) {
            case R.id.close_imageview:
            case R.id.submit_textview:
                finish();
                break;
            case R.id.find_textview:
                //找回密码
                findBack();
                break;
        }
    }

    private void overOprate() {
        notice1_view.setVisibility(View.GONE);
        notice2_view.setVisibility(View.VISIBLE);
        email_textview.setText(email);
        progress_line.setBackgroundColor(BLUE);
        input_view.setVisibility(View.GONE);
        email_line.setVisibility(View.GONE);
        find_textview.setVisibility(View.GONE);
        success_view.setVisibility(View.VISIBLE);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imm = (InputMethodManager) this
                .getSystemService(Context.INPUT_METHOD_SERVICE);

        setListener();
        isEnable();
    }

    private void setListener() {

        //判断是否获取焦点 - email
        email_edittext.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    email_line.setBackgroundColor(BLUE);
                } else {
                    // 此处为失去焦点时的处理内容
                    email_line.setBackgroundColor(GRAY);
                    checkContent();
                }
            }
        });

        //判断内容 - email
        email_edittext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    email_warning.setVisibility(View.VISIBLE);
                    email_warning.setTextColor(GRAY);
                    email_warning.setText("电子邮箱");
                } else {
                    email_warning.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                isEnable();
            }
        });

    }

    private void isEnable() {
        int errors = 0;
        email = email_edittext.getText().toString();

        //电子邮箱
        if (AppUtils.isEmpty(email)) {
            errors++;
        } else if (!AppUtils.isEmail(email)) {
            errors++;
        }

        if (errors == 0) {
            find_textview.setEnabled(true);
            find_textview.setBackgroundColor(BLUE);
        } else {
            find_textview.setEnabled(false);
            find_textview.setBackgroundColor(UNCLICK);
        }
    }

    //校验输入内容
    private void checkContent() {

        int errors = 0;

        email = email_edittext.getText().toString();

        //电子邮箱
        if (AppUtils.isEmpty(email)) {
            email_warning.setVisibility(View.GONE);
            errors++;
        } else if (!AppUtils.isEmail(email)) {
            email_warning.setVisibility(View.VISIBLE);
            email_warning.setTextColor(RED);
            email_warning.setText("请输入有效的邮箱");
            errors++;
        } else {
            email_warning.setVisibility(View.VISIBLE);
            email_warning.setTextColor(GRAY);
            email_warning.setText("电子邮箱");
        }

        if (errors == 0) {
            find_textview.setEnabled(true);
            find_textview.setBackgroundColor(BLUE);
        } else {
            find_textview.setEnabled(false);
            find_textview.setBackgroundColor(UNCLICK);
        }
    }

    // 点击EditText以外的任何区域隐藏键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (v != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                checkContent();
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            checkContent();
        }
        return super.onTouchEvent(event);
    }

    //找回密码
    private void findBack() {
        loadingDialog.show();
        Map<String, Object> map = new HashMap<>();
        map.put("email", email);

        MyOkHttpUtils.getServiceDatas(map, Contents.MODIFYPASSWD, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void success(String response, int d) {
                if (response != null) {
                    JsonAnalysisModel jam = JsonAnalysisUtils.jsonAnalysis(response, CurrencyModel.class);
                    if (jam.isSuccess()) {
                        CurrencyModel model = (CurrencyModel) jam.getData();
                        if (model.getErr_code().equals("0")) {
                            overOprate();
                        }else{
                            ToastUtils.showToast(jam.getMsg());
                        }
                    } else {
                        ToastUtils.showToast(jam.getMsg());
                    }
                }
                loadingDialog.dismiss();
            }

            @Override
            public void faild(Call call, Exception e, int id) {
                loadingDialog.dismiss();
            }
        });
    }
}
